package com.daocao.ds.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.RuleSwitchBean;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.util.Dialog;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.Util;
import com.daocao.ds.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SnatchingModeActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.btn_order_open)
    ImageView btnOrderOpen;
    @BindView(R.id.btn_gesture_pass)
    LinearLayout btnGesturePass;
    @BindView(R.id.btn_order_mode)
    LinearLayout btnOrderMode;

    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snatching_mode);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("抢单模式");

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserInfoGlobal.getAutoRule()) {
            btnOrderOpen.setSelected(true);
        } else {
            btnOrderOpen.setSelected(false);
        }
    }

    public void getRuleSwitch(boolean oepn) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        RuleSwitchBean bean = new RuleSwitchBean();
        if (oepn) {
            bean.open = "ON";
        } else {
            bean.open = "OFF";
        }
        Api.api_service.getRuleSwitch(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (oepn) {
                        btnOrderOpen.setSelected(true);
                        ToastUtil.makeText(this, "开启成功");
                    } else {
                        btnOrderOpen.setSelected(false);
                        ToastUtil.makeText(this, "关闭成功");
                    }
                    UserInfoGlobal.saveAutoRule(bean.open);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_order_open, R.id.btn_order_mode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_order_open:
                if (UserInfoGlobal.getAutoRule()) {
                    Dialog.showDefaultDialog(this, "", "关闭自动抢单模式后，不能及时抢单喔，您确定要关闭吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            getRuleSwitch(false);
                        }

                        @Override
                        public void cancel() {

                        }
                    });
                } else {
                    Util.startActivity(this, SnatchingModeSettingActivity.class);
                }
                break;
            case R.id.btn_order_mode:
                Util.startActivity(this, SnatchingModeSettingActivity.class);
                break;
        }
    }
}
